﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class LevelNotic : MonoBehaviour
{
    public GameObject player;
    /// <summary>
    /// 终点，由gameControl赋值
    /// </summary>
    public GameObject ending;
    private Vector3 initialPosition;
    private float distance;
    //private float curDistance;

    public Text textCurLevel;
    public Text textNextLevel;
    public Slider slider;

    private void OnEnable()
    {
        int levelNum = User.instance.CurLevel;

        initialPosition = player.transform.position;
        textCurLevel.text = (levelNum + 1).ToString();
        textNextLevel.text = (levelNum + 2).ToString();
        distance = EndingDistance();

    }

    private void Update()
    {
        slider.value = Mathf.Abs(player.transform.position.x - initialPosition.x) / distance;
    }


    /// <summary>
    /// 计算玩家和终点的距离
    /// </summary>
    /// <returns></returns>
    public float EndingDistance()
    {
        if (player && ending)
        {

            return (Mathf.Abs(ending.transform.position.x - player.transform.position.x));
        }
        else
        {
            return 22f;
        }
    }


}
